package main

import (
	"fmt"
	"net"
	"os"
	"io/ioutil"
)

func main(){
//	ParseIP("10.99.206.153")
	TCPClientDemo("127.0.0.1:7070")
}

func ParseIP(ipstr string){
//	ipstr:="10.99.206.153"
	ip:=net.ParseIP(ipstr)
	if ip==nil {
		fmt.Println("invalid ip address")
	}else{
		fmt.Println("resolve ip:",ip.String())
	}
}

func TCPClientDemo(service string){
	fmt.Println("----------TCPClientDemo----------")
	tcpAddr,err:=net.ResolveTCPAddr("tcp4",service)
	checkError(err)
	conn,err:=net.DialTCP("tcp",nil,tcpAddr)
	checkError(err)
	_,err=conn.Write([]byte("HEAD / HTTP/1.0\r\n\r\n"))
	checkError(err)
	fmt.Println("start to read")
	result,err:=ioutil.ReadAll(conn)
	fmt.Println("end of read")
	checkError(err)
	fmt.Println(string(result))
	os.Exit(0)
}

func checkError(err error){
	if err != nil {
		fmt.Fprintf(os.Stderr,"Fatail error:%s",err.Error())
		panic(err)
	}
}

